<template>
  <d2-container class="page">
    <el-dialog
      width="30%"
      title="审核评论"
      :visible.sync="dialogFormVisible2"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-select v-model="value" placeholder="请选择">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="audit()">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :title="createOrEdit"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form :model="form" ref="form1">
        <el-form-item label="昵称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.nickName"></el-input>
        </el-form-item>
        <el-form-item label="头像" :label-width="formLabelWidth">
          <el-upload
            action="https://xcx.ruiysoft.com/reserve/qiniu/upload"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
            :limit="1"
            :on-success="handleUploadSuccess"
            multiple
            :file-list="fileList"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="内容" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.commentContent" :rows="5" placeholder="请输入内容" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleCreateNew('form1')">确 定</el-button>
      </div>
    </el-dialog>
    <el-button type="primary" size="mini" @click="showCreateDlg('form1')">新建</el-button>
    <el-button type="primary" size="mini" @click="search()">搜索</el-button>
    <el-divider></el-divider>
    <el-table
      stripe
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="ID">
        <template slot-scope="scope">{{ scope.row.id }}</template>
      </el-table-column>
      <el-table-column label="SPU">
        <template slot-scope="scope">{{ scope.row.spuId }}</template>
      </el-table-column>
      <!-- <el-table-column label="名称">
        <template slot-scope="scope">{{ scope.row.merchandiseName }}</template>
      </el-table-column>-->
      <el-table-column label="昵称" width="250">
        <template slot-scope="scope">{{ scope.row.nickName }}</template>
      </el-table-column>
      <el-table-column label="头像">
        <template slot-scope="scope">
          <img v-bind:src="scope.row.headImgUrl" style="width:55%">
        </template>
      </el-table-column>
      <el-table-column label="内容">
        <template slot-scope="scope">{{ scope.row.commentContent}}</template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">{{ scope.row.commentStatus | auditStatus}}</template>
      </el-table-column>
      <el-table-column label="创建时间" width="200">
        <template
          slot-scope="scope"
        >{{ scope.row.createDate | format2DateTime('{y}-{m}-{d} {h}:{m}:{s}') }}</template>
      </el-table-column>
      <el-table-column label="更新时间" width="200">
        <template
          slot-scope="scope"
        >{{ scope.row.modifyDate | format2DateTime('{y}-{m}-{d} {h}:{m}:{s}') }}</template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="200px" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="showEditDlg(scope.row)" icon="el-icon-edit"></el-button>
          <el-button size="mini" type="success" @click="showAuditDlg(scope.row)">审核</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)"
            icon="el-icon-delete"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <br>
    <el-pagination background="" layout="prev, pager, next" :total="50"></el-pagination>
  </d2-container>
</template>

<script>
import * as comment from "@/api/shop.comment";
import { rejects } from "assert";

export default {
  components: {},
  data() {
    return {
      selectedRow: null,
      value: null,
      options: [{
          value: '0',
          label: '通过'
        }, {
          value: '1',
          label: '审核中'
        }, {
          value: '2',
          label: '未通过'
        }],
      dialogFormVisible2: false,
      fileList: [],
      tableData: [],
      multipleSelection: [],
      dialogFormVisible: false,
      form: {
        quantity: 999,
        listPrice: 0,
        costPrice: 0
      },
      formLabelWidth: "200px",
      dialogImageUrl: "",
      dialogVisible: false,
      createOrEdit: "",
      downloadPath: "",
      listQuery: {
        pageNum: 1,
        pageSize: 25,
        spu_id: this.$route.query.spuId
      },
      categoryList: null,
      aftersaleConfList: null,
      dialogStandardVisible: false
    };
  },
  mounted() {
    this.$route.meta.title = this.$route.query.spuName + " - 评论列表";
  },

  created() {
    this.tableData = [];
    this.getList();
  },

  filters: {
    auditStatus: function(val) {
      if (val == 0) return "通过";
      if (val == 1) return "审核中";
      if (val == 2) return "未通过";
    },

    format2DateTime: function(time, cFormat) {
      time = Date.parse(new Date(time));
      if (arguments.length === 0) {
        return null;
      }

      if ((time + "").length === 10) {
        time = +time * 1000;
      }

      const format = cFormat || "{y}-{m}-{d} {h}:{i}:{s}";
      let date;
      if (typeof time === "object") {
        date = time;
      } else {
        date = new Date(parseInt(time));
      }
      const formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay()
      };
      const timeStr = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
        let value = formatObj[key];
        if (key === "a")
          return ["一", "二", "三", "四", "五", "六", "日"][value - 1];
        if (result.length > 0 && value < 10) {
          value = "0" + value;
        }
        return value || 0;
      });
      return timeStr;
    }
  },

  methods: {
    showAuditDlg(row) {
      this.dialogFormVisible2 = true;
      this.selectedRow = row;
    },

    audit() {
      let that = this;

      that.selectedRow.commentStatus = this.value;
      comment.update(that.selectedRow).then(resp => {
        if (resp.success) {
          this.dialogFormVisible2 = false;
          that.getList();
          that.$notify({
            title: "提示",
            message: "操作成功",
            type: "success",
            duration: 2000
          });
        } else {
          that.$notify({
            title: "提示",
            message: "异常: " + resp.bizMessage,
            type: "error",
            duration: 2000
          });
        }
      });
    },

    search() {
      this.getList();
    },

    showCreateDlg(formName) {
      this.dialogFormVisible = true;
      this.createOrEdit = "新建";
      this.form = {};
      this.form.spuId = this.$route.query.spuId;
      this.form.merchandiseRuleValue = {};
    },

    showEditDlg(row) {
      this.dialogFormVisible = true;
      this.createOrEdit = "编辑";

      this.form = row;
      this.fileList = [{url: row.headImgUrl}];
      // this.form.listMerchandisePictureSku.forEach(element => {
      //   element.url = element.path;
      //   this.fileList.push(element);
      // });
    },

    handleUploadSuccess(response, file, fileList) {
      console.log(response);
      if (response.success)
        this.fileList.push({
          url: response.downloadPath,
          path: response.downloadPath,
          sort: 0,
          type: 1,
          delFlag: 0
        });
    },

    handleCreateNew(formName) {
      let that = this;

      const set = that.$refs;
      set[formName].validate(valid => {
        if (valid) {
          that.form.headImgUrl = that.fileList[0].url;

          if (that.createOrEdit == "新建") {
            that.form.spuId = this.$route.query.spuId;
            comment.insert(that.form).then(resp => {
              if (resp.success) {
                that.dialogFormVisible = false;
                that.getList();
                that.$notify({
                  title: "提示",
                  message: "操作成功",
                  type: "success",
                  duration: 2000
                });
              } else {
                that.$notify({
                  title: "提示",
                  message: "异常: " + resp.bizMessage,
                  type: "error",
                  duration: 2000
                });
              }
            });
          }

          if (that.createOrEdit == "编辑") {
            comment.update(that.form).then(resp => {
              if (resp.success) {
                that.dialogFormVisible = false;
                that.getList();
                that.$notify({
                  title: "提示",
                  message: "操作成功",
                  type: "success",
                  duration: 2000
                });
              } else {
                that.$notify({
                  title: "提示",
                  message: "异常: " + resp.bizMessage,
                  type: "error",
                  duration: 2000
                });
              }
            });
          }
        } else {
          return false;
        }
      });
    },

    getList() {
      comment.fetchList(this.listQuery).then(resp => {
        console.log(resp);
        this.tableData = resp.data.records;
      });
    },

    handleRemove(file, fileList) {
      console.log(file, fileList);
      const index = this.fileList.indexOf(file, 0);
      if (index > -1) {
        this.fileList.splice(index, 1);
      }
    },

    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },

    handleDelete(row) {
      this.$confirm("确认删除么?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        comment.deleteById(row.id).then(resp => {
          this.getList();
          this.$notify({
            title: "成功",
            message: "删除成功",
            type: "success",
            duration: 2000
          });
        });
      });
    }
  }
};
</script>